<template>
  <div class="login-container">
    <h1>登录</h1>
    <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="120px">
      <el-form-item label="登录类型" prop="userType">
        <el-radio-group v-model="loginForm.userType" @change="updatePlaceholders">
          <el-radio-button label="user">普通用户</el-radio-button>
          <el-radio-button label="admin">管理员</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="type" prop="username">
        <el-input v-model="loginForm.username" :placeholder="usernamePlaceholder"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin" class="login-button">登录</el-button>
        <el-button type="primary" @click="handleRegister" class="register-button">现在注册</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" @click="handleResetPassword">忘记密码？</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "LoginContent",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        userType: "user", // 默认选择普通用户
      },
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {min: 6, message: "密码长度不能少于6位", trigger: "blur"},
        ],
        userType: [
          {required: true, message: "请选择登录类型", trigger: "change"},
        ],
      },
      usernamePlaceholder: "请输入用户名", // 默认提示信息
      type: "用户名",
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          console.log("登录成功", this.loginForm);
          this.$store.state.userType = this.loginForm.userType; // 更新全局变量
          this.$store.state.isAuthenticated = true;
          this.$router.push('/'); // 跳转到首页
        } else {
          console.log("表单验证失败");
        }
      });
    },
    handleRegister() {
      this.$router.push('/register'); // 假设有一个注册页面路由
    },
    updatePlaceholders() {
      // 根据用户类型更新用户名输入框的提示信息
      if (this.loginForm.userType === 'user') {
        this.usernamePlaceholder = "请输入用户名";
        this.type = "用户名";
      } else if (this.loginForm.userType === 'admin') {
        this.usernamePlaceholder = "请输入管理员账号";
        this.type = "管理员账号";
      }
    },
    handleResetPassword() {
      // 重置密码逻辑
      console.log("跳转到重置密码页面");
      this.$router.push('/reset-password'); // 假设有一个重置密码页面路由
    },
  },
  mounted() {
    // 初始化时设置默认提示信息
    this.updatePlaceholders();
  },
};
</script>

<style scoped>
.login-container {
  width: 100%;
  max-width: 400px;
  margin: 50px auto;
  padding: 20px;
  border-radius: 5px;
  background-color: white;
}

.login-button, .register-button {
  width: 120px; /* 设置按钮宽度一致 */
}
</style>